package zoarEE;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.*;
/**
 * Servlet implementation class login
 */
@WebServlet("/login")
public class login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();		
		out.write("abnormal you");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String UserName= request.getParameter("UserName");
			String Password=request.getParameter("Password");
			PrintWriter out = response.getWriter();			
			try {
				Connection conn= DB.Connect();
		        String sql = "select * from Users where UserName=? and Password=?";

		        PreparedStatement pstat = conn.prepareStatement(sql);
		        pstat.setString(1, UserName);
		        pstat.setString(2, Password);
		        
		        ResultSet result = pstat.executeQuery();
		        if(!result.next ())
		        {
		        	out.print("Account not exist or wrong password");
		        }
		        else
		        {
		        	out.print("hello, "+UserName);
		        }
		        conn.close();
		        pstat.close();
		        } catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}


}
